package com.justgame.project.cloud.common.config;

import com.aliyun.oss.ClientBuilderConfiguration;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * oss对象存储
 */
@Configuration
@Slf4j
@RefreshScope
public class OssConfig {
    @Value("${endpoint}")
    private String endpoint;
    @Value("${accessKeyId}")
    private String accessKeyId;
    @Value("${secret}")
    private String secretAccessKey;

    @Bean
    public OSS oss() {
        try{
            ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
            conf.setSupportCname(true);
            return new OSSClientBuilder().build(
                    endpoint,
                    accessKeyId,
                    secretAccessKey,
                    conf
            );
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
